#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using NUnit.Framework;
using EngineOfEvermore.Scene;
using EngineOfEvermore.Common;
using EngineOfEvermore.Scene.Visual;
using EngineOfEvermore.Scene.Physics;
using EngineOfEvermore.Scene.ObjectLibrary;
using EngineOfEvermore.Imaging;

using System.Xml;
using System.Xml.Schema;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Drawing;
using Blue.ResourceManager;

using Green.Logging;


namespace EngineOfEvermore.Scene.Test
{
    /*
	#if ENGINE_OF_EVERMORE_ENABLE_UNIT_TESTS
	[TestFixture()]
	public class SceneObjectPersistence
	{
		
		private const string SCENEOBJECT_DEFINITION_RESOURCENAME =  "sceneObjectDefinition.def";
		private const string SCENEOBJECT_RESOURCENAME = "sceneObject.xso";
		
		const String SOMBOY_IMAGE_MAP = "ImageMaps/somsheet.ximp";

        const String WALK_RIGHT_ANIM = "Animations/SomBoyWalkRight.xani";
        const String WALK_UP_ANIM = "Animations/SomBoyWalkUp.xani";
        const String WALK_DOWN_ANIM = "Animations/SomBoyWalkDown.xani";

        const String IDLE_RIGHT_ANIM = "Animations/SomBoyIdleRight.xani";
        const String IDLE_UP_ANIM = "Animations/SomBoyIdleUp.xani";
        const String IDLE_DOWN_ANIM = "Animations/SomBoyIdleDown.xani";

		[SetUp]
		public void init() 
		{
			ResourceManager.Initialize( new FileSystemResourceManager( "Data" ) );
			Scene.Initialization.Init();
			EngineOfEvermore.Common.Initialization.Init();
			Imaging.Initialization.Init();
			new LogTextWriter( System.Console.Out );
			
			XmlSerialization.AddSchemaPath( "../../../../Common/Data/XmlSchema" );
			XmlSerialization.AddSchemaPath( "../../../Data/XmlSchema" );
		}

        [TearDown]
        public void TearDown()
        {
            ResourceManager.Dispose();
        }
		
        
//		[Ignore]
		[Test]
		public void TestSceneObjectSerialization() {
			
			// create a sceneObject with some values
			SceneObject so1 = createSceneObject1();
				
			SceneObjectResource.SaveResourceAndReplace( SCENEOBJECT_RESOURCENAME, so1 );

			SceneObjectResource soRes = ResourceManager.GetResource<SceneObjectResource>( SCENEOBJECT_RESOURCENAME );
			SceneObject so2 = soRes.SceneObject;
			ResourceManager.DeleteResource( SCENEOBJECT_RESOURCENAME );
			
			// check if we have got a vaild SceneObject
			Assert.IsNotNull( so2, "deserialized SceneObject is null -> could not load object" );
			
			// check if the sceneObject property values are the same
			Assert.AreEqual(so1.Rotation, so2.Rotation);
			Assert.AreEqual(so1.Space, so2.Space);
			Assert.AreEqual(so1.Position, so2.Position);
			Assert.AreEqual(so1.LinearVelocity, so2.LinearVelocity);
			Assert.AreEqual(so1.ComponentCount, so2.ComponentCount);

		}
		
		
		[Test]
		public void TestSceneObjectDefinitionSerialization()
		{
			SceneObject so = createSceneObject1();

			SceneObjectDefinition.SaveResourceAndReplace( SCENEOBJECT_DEFINITION_RESOURCENAME, so );
			
			SceneObjectDefinition def = ResourceManager.GetResource<SceneObjectDefinition>( SCENEOBJECT_DEFINITION_RESOURCENAME );
			ResourceManager.DeleteResource( SCENEOBJECT_DEFINITION_RESOURCENAME );
			Assert.IsNotNull( def, "could not load SceneObjectDefinition" );
			so = def.CreateSceneObject();
			Assert.IsNotNull( so );
			Assert.AreEqual( so.Position, new Vector2d( 0, 0 ) );
			Assert.AreEqual( so.LinearVelocity, new Vector2d( 0, 0 ) );
			Assert.AreEqual( so.Rotation, 0.0f );
			Assert.AreEqual( so.Space, 0 );
			
		}
		
		
		[Test]
		public void TestCollisionComponentSerialization() {

			CollisionComponent cc = createCollisionComponent1();
			
			XmlSerialization s = new XmlSerialization();
			System.IO.TextWriter tw1 = new System.IO.StringWriter();
			s.AddObject( cc );
			
			s.Save( tw1 );
//			s.Save(Console.Out);
			
			CollisionComponent cc2 = s.GetObject<CollisionComponent>( 0 );
			
			
			Assert.AreEqual(cc.Weight, cc2.Weight);
			
			System.IO.TextWriter tw2 = new System.IO.StringWriter();
			s.Clear();
			s.AddObject( cc2 );
			s.Save( tw2 );
//			s.Save( Console.Out );
			
			Assert.That(tw1.ToString().Equals(tw2.ToString()));
			
		}
		
		
	
		[Ignore] // not testable without agatelib surface :(
		[Test]
		public void AnimatedSpriteSerialization() {
            AnimatedSprite animatedSprite = new AnimatedSprite();
            animatedSprite.Animations["IdleRight"] =
                ResourceManager.GetResource<FrameAnimation>( IDLE_RIGHT_ANIM );
            animatedSprite.Animations["IdleUp"] =
                ResourceManager.GetResource<FrameAnimation>( IDLE_UP_ANIM );
            animatedSprite.Animations["IdleDown"] =
                ResourceManager.GetResource<FrameAnimation>( IDLE_DOWN_ANIM );

            animatedSprite.Animations["WalkRight"] =
                ResourceManager.GetResource<FrameAnimation>( WALK_RIGHT_ANIM );
            animatedSprite.Animations["WalkUp"] =
                ResourceManager.GetResource<FrameAnimation>( WALK_UP_ANIM );
            animatedSprite.Animations["WalkDown"] =
                ResourceManager.GetResource<FrameAnimation>( WALK_DOWN_ANIM );

			
            animatedSprite.Scale = new Vector2d( 2, 2 );
            animatedSprite.PositionOffset = new Vector2d( 0, -20 );
            animatedSprite.CurrentAnimation = "IdleRight";
            animatedSprite.Name = "main";

			SceneObject so1 = new SceneObject();
			VisualComponent c = new VisualComponent();
			c.Children.Add( animatedSprite );
			so1.AddComponent( c );

			SceneObjectResource.SaveResourceAndReplace( SCENEOBJECT_RESOURCENAME, so1 );

			SceneObjectResource res = ResourceManager.GetResource<SceneObjectResource>( SCENEOBJECT_RESOURCENAME );
			Assert.IsNotNull( res );
			Assert.IsNotNull( res.SceneObject );
            SceneObject so2 = res.SceneObject;

			XmlSerialization s2 = new XmlSerialization();
			s2.AddObject( so2 );
			s2.Save( Console.Out );

		}

		
		
		[Test]
		public void TestPerInstanceModeSerializationWithSceneObject()
		{
			SceneObject o = createSceneObject1();
			XmlSerialization s = new XmlSerialization();
			s.SerializationType = SerializationTypes.perInstance;
			
			s.AddObject( o );
			
//			s.Save( Console.Out );
			XDocument d = s.Document;
			
			XElement selem = d.Element( "EoE" ).Element( "SceneObject" );
			Assert.IsNotNull( selem );
			Assert.IsNotNull( selem.Element( "Position" ) );
			Assert.IsNotNull( selem.Element( "Rotation" ) );
			Assert.IsNotNull( selem.Element( "LinearVelocity" ) );
			Assert.IsNotNull( selem.Element( "Space" ) );
			Assert.IsFalse( selem.Element( "Components" ).HasElements );
			
		}
		
		
		
		
		[Test]
		public void TestSceneObjectDeserializationWithDefinitionReference()
		{
			SceneObject sdef = createSceneObject1();
			
			SceneObjectDefinition.SaveResourceAndReplace( SCENEOBJECT_DEFINITION_RESOURCENAME, sdef );
			
			XDocument doc = new XDocument(
			        new XElement( "EoE",
			               new XElement( "SceneObject",
			                      new XElement( "SceneObjectDefinition", SCENEOBJECT_DEFINITION_RESOURCENAME ),
			                      new XElement( "Position",
			                             new XElement( "X", 77 ),
			                             new XElement( "Y", 88 )
			                      ),
			                      new XElement( "Rotation", 99.99f ),
			                      new XElement( "LinearVelocity",
			                             new XElement( "X", 22 ),
			                             new XElement( "Y", 11 )
			                      ),
			                      new XElement( "Space", 55 ),
			                      new XElement( "Components" ),
			                      new XElement( "PhysicsModifier" )
			               )
			        )
			    );
			
			XmlSerialization s = new XmlSerialization();
			s.Document = doc;
//			s.Save( Console.Out );
			
			Assert.AreNotEqual( 0, s.GetObjectCount() );
			
			SceneObject so = s.GetObject<SceneObject>( 0 );
			
			ResourceManager.DeleteResource( SCENEOBJECT_DEFINITION_RESOURCENAME );
			
			Assert.IsNotNull( so );
			
			Assert.AreEqual( 77, so.Position.X );
			Assert.AreEqual( 88, so.Position.Y );
			Assert.AreEqual( 99.99f, so.Rotation );
			Assert.AreEqual( 22, so.LinearVelocity.X );
			Assert.AreEqual( 11, so.LinearVelocity.Y );
			Assert.AreEqual( 55, so.Space );
			
			VisualComponent vc = so.GetComponent<VisualComponent>();
			Assert.IsNotNull( vc );
			Assert.AreEqual( 5.5f, vc.PositionOffset.X );
			Assert.AreEqual( 6.6f, vc.PositionOffset.Y );
			
			CollisionComponent cc = so.GetComponent<CollisionComponent>();
			Assert.IsNotNull( cc );
			Assert.AreEqual( 8.8f, cc.Weight );
			Assert.AreEqual( 1, cc.Shapes.Count );
		}
		
		
		[Test]
		public void TestSceneObjectInstanzHasComponentWhichObjectDefinitionHas()
		{
			SceneObject sdef = createSceneObject1();
			sdef.AddComponent( new TestSceneObjectComponent() );
			
			XmlSerialization.ValidationEnabled = false;
			XmlSerialization.AddTypeInformation( typeof(TestSceneObjectComponent).Name, typeof(TestSceneObjectComponent).AssemblyQualifiedName );
			
			SceneObjectDefinition.SaveResourceAndReplace( SCENEOBJECT_DEFINITION_RESOURCENAME, sdef );
			
			XDocument doc = new XDocument(
			        new XElement( "EoE",
			               new XElement( "SceneObject",
			                      new XElement( "SceneObjectDefinition", SCENEOBJECT_DEFINITION_RESOURCENAME ),
			                      new XElement( "Position",
			                             new XElement( "X", 77 ),
			                             new XElement( "Y", 88 )
			                      ),
			                      new XElement( "Rotation", 99.99f ),
			                      new XElement( "LinearVelocity",
			                             new XElement( "X", 22 ),
			                             new XElement( "Y", 11 )
			                      ),
			                      new XElement( "Space", 55 ),
			                      new XElement( "Components", 
			                      		new XElement( "TestSceneObjectComponent" )
			                      ),
			                      new XElement( "PhysicsModifier" )
			               )
			        )
			    );
			
			XmlSerialization s = new XmlSerialization();
			s.Document = doc;
			
			//s.Save( Console.Out );
			
			Assert.AreNotEqual( 0, s.GetObjectCount() );
			
			try {
				SceneObject so = s.GetObject<SceneObject>( 0 );
			} catch (ArgumentException) {
				ResourceManager.DeleteResource( SCENEOBJECT_DEFINITION_RESOURCENAME );
				XmlSerialization.ValidationEnabled = true;
				
				return;
			}
			
			Assert.Fail( "no ArgumentException was thrown" );
		}
		
		
		
		[Test]
		public void TestSceneObjectSerializationWithSceneObjectDefinitionRefComponent()
		{
			XmlSerialization ser = new XmlSerialization();
			
			// create sceneobject definition 
			SceneObject sdef = createSceneObject1();
			SceneObjectDefinition.SaveResourceAndReplace( SCENEOBJECT_DEFINITION_RESOURCENAME, sdef );
			
			// create a sceneObject instance with referece component to the definition
			SceneObject soInst = createSceneObject1();
			soInst.Position = new Vector2d( 77, 88 );
			soInst.Rotation = 99.99f;
			soInst.LinearVelocity = new Vector2d( 22, 11 );
			soInst.Space = 55;
			soInst.AddComponent( new SceneObjectDefinitionRefComponent( SCENEOBJECT_DEFINITION_RESOURCENAME ) );
			
			SceneObjectResource.SaveResourceAndReplace( SCENEOBJECT_RESOURCENAME, soInst );
			
			SceneObjectResource res = ResourceManager.GetResource<SceneObjectResource>( SCENEOBJECT_RESOURCENAME );
			Assert.IsNotNull( res );
			SceneObject so = res.SceneObject;
			Assert.IsNotNull( so );
			
			Assert.AreEqual( 77, so.Position.X );
			Assert.AreEqual( 88, so.Position.Y );
			Assert.AreEqual( 99.99f, so.Rotation );
			Assert.AreEqual( 22, so.LinearVelocity.X );
			Assert.AreEqual( 11, so.LinearVelocity.Y );
			Assert.AreEqual( 55, so.Space );
			
			VisualComponent vc = so.GetComponent<VisualComponent>();
			Assert.IsNotNull( vc );
			Assert.AreEqual( 5.5f, vc.PositionOffset.X );
			Assert.AreEqual( 6.6f, vc.PositionOffset.Y );
			
			CollisionComponent cc = so.GetComponent<CollisionComponent>();
			Assert.IsNotNull( cc );
			Assert.AreEqual( 8.8f, cc.Weight );
			Assert.AreEqual( 1, cc.Shapes.Count );
		}
		
		
		
		[Test]
		public void TestPoligonShapeSerialization()
		{
			SceneObject so = createSceneObject1();
			CollisionComponent cc = so.GetComponent<CollisionComponent>();
			cc.Shapes.Clear();
			List<Vector2d> vert = new List<Vector2d>();
			vert.Add( new Vector2d( 1, 2 ) );
			vert.Add( new Vector2d( 3, 4 ) );
			vert.Add( new Vector2d( 5, 6 ) );
			PolygonShape ps = new PolygonShape( vert );
			ps.CollisionGroup = "group";
			cc.Shapes.Add( ps );
			
			SceneObjectResource.SaveResourceAndReplace( SCENEOBJECT_RESOURCENAME, so );
			
			SceneObjectResource res = ResourceManager.GetResource<SceneObjectResource>( SCENEOBJECT_RESOURCENAME );
			
			Assert.IsNotNull( res );
			Assert.IsNotNull( res.SceneObject );
			so = res.SceneObject;
			
			cc = so.GetComponent<CollisionComponent>();
			Assert.IsNotNull( cc );
			Assert.AreEqual( 1, cc.Shapes.Count );
			
			IEnumerator<CollisionShape> it = cc.Shapes.GetEnumerator();
			
			it.MoveNext();
			Assert.IsNotNull( it.Current );
			ps = (PolygonShape) it.Current;
			Assert.AreEqual( "group", ps.CollisionGroup );
			
			IEnumerator<Vector2d> v = ps.Vertices.GetEnumerator();
			
			v.MoveNext();
			Assert.AreEqual( new Vector2d( 1, 2 ), v.Current );
			v.MoveNext();
			Assert.AreEqual( new Vector2d( 3, 4 ), v.Current );
			v.MoveNext();
			Assert.AreEqual( new Vector2d( 5, 6 ), v.Current );
		}
		
		
		
		[Test]
		public void TestRectangleShapeSerialization()
		{
			SceneObject so = createSceneObject1();
			CollisionComponent cc = so.GetComponent<CollisionComponent>();
			cc.Shapes.Clear();
			RectangleShape cs = new RectangleShape( new RectangleF( 1, 2, 3, 4 ) );
			cs.CollisionGroup = "group";
			cc.Shapes.Add( cs );
			
			SceneObjectResource.SaveResourceAndReplace( SCENEOBJECT_RESOURCENAME, so );
			
			SceneObjectResource res = ResourceManager.GetResource<SceneObjectResource>( SCENEOBJECT_RESOURCENAME );
			
			Assert.IsNotNull( res );
			Assert.IsNotNull( res.SceneObject );
			so = res.SceneObject;
			
			cc = so.GetComponent<CollisionComponent>();
			Assert.IsNotNull( cc );
			Assert.AreEqual( 1, cc.Shapes.Count );
			
			IEnumerator<CollisionShape> it = cc.Shapes.GetEnumerator();
			
			it.MoveNext();
			Assert.IsNotNull( it.Current );
			cs = (RectangleShape) it.Current;
			Assert.AreEqual( new RectangleF( 1, 2, 3, 4 ) , cs.Rectangle );
			Assert.AreEqual( "group", cs.CollisionGroup );
		}
		
		
		
		
		private SceneObject createSceneObject1() 
		{
			SceneObject so1 = new SceneObject();
			so1.Position = new Vector2d(11.1f, 22.2f);
			so1.LinearVelocity = new Vector2d(33.3f, 44.4f);
			so1.Rotation = 1.23f;
			so1.Space = 2;
			
			VisualComponent vc = new VisualComponent();
			vc.PositionOffset = new Vector2d(5.5f, 6.6f);
			vc.RotationOffset = 7.7f;

			// not testable without agatelib surface :(
//			StaticSprite ss = new StaticSprite();
//			ss.ClipArea = new System.Drawing.RectangleF(1.1f, -2.2f, 10.01f, 20.02f);
//			ss.Scale = new Vector2d(1,2);
//			ss.FlipX = true;
//			ss.Name = "My static sprite";
//			ss.ImageMap = ResourceManager.GetResource<ImageMap>( "ImageMaps/arrow.ximp" );
//			vc.Children.Add(ss);
			
			
			CollisionComponent cc = createCollisionComponent1();
			cc.Weight = 8.8f;
			
			so1.AddComponent(vc);
			so1.AddComponent(cc);
			
			return so1;
		}
		
		
		private CollisionComponent createCollisionComponent1()
		{
			CollisionComponent cc = new CollisionComponent();
			cc.Weight = 88.8f;
			System.Drawing.RectangleF rect = new System.Drawing.RectangleF( 2f, 3f, 43.5f, 65.23f );
			RoundedRectangleShape rrs = new RoundedRectangleShape( rect, 5.5f );
			rrs.CollisionGroup = "my group";
			
			cc.Shapes.Add( rrs );
			
			return cc;
		}
		
		
		
		internal class TestSceneObjectComponent : SceneObjectComponent {
			public override SceneObjectComponent Clone() {
				return new TestSceneObjectComponent();
			}
		}
		
	}
    #endif
    */
}